define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'registr_activity/index' + location.search,
                    add_url: 'registr_activity/add',
                    edit_url: 'registr_activity/edit',
                    del_url: 'registr_activity/del',
                    multi_url: 'registr_activity/multi',
                    import_url: 'registr_activity/import',
                    table: 'registr_activity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'area_name', title: '所屬區域', operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'join_nun', title: __('Join_nun'),sortable: true, operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'),sortable: true, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:function(value,row,index){
                            this.buttons = [
                                {
                                    name: 'activity_code',
                                    text: '保存二維碼',
                                    title: '保存二維碼',
                                    classname: 'btn btn-xs btn-info',
                                    extend:'download="活動_'+row.id+'.png"',
                                    icon: '',
                                    url: function (row){
                                    // row 为行数据
                                      return row.code;
                                    } ,
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if ( Config.activity_code ) {
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                            
                                {
                                    name: 'activity_user',
                                    text: '歷史登記列表',
                                    title: '歷史登記列表',
                                    classname: 'btn btn-xs btn-info btn-addtabs',
                                    icon: '',
                                    url: 'registr_activity_user/index',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if ( Config.activity_user ) {
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                 {
                                    name: 'activity_user',
                                    text: '下載列表',
                                    title: '下載列表',
                                    classname: 'btn btn-xs btn-info',
                                    icon: '',
                                    url: 'registr_activity/get_questionnaire_excel_file',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if ( Config.activity_xiazai ) {
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                {
                                    name: 'edit',
                                    text: '編輯',
                                    title: '編輯',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: '',
                                    url: 'registr_activity/edit',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if ( Config.activity_edit ) {
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                {
                                    name: 'del',
                                    text: '刪除',
                                    title: '刪除',
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    icon: '',
                                    url: 'registr_activity/del',
                                    confirm: '確認刪除嗎?',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if ( Config.activity_del ) {
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                }
                                ];
                                return Table.api.formatter.buttons.call(this,value,row,index)
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'registr_activity/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'registr_activity/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'registr_activity/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});